package Hot100.LinkNode;

import DMSXL._2023Before.ListNode_.ListNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class sortList {
    public static void main(String[] args) {
        int[] listA = {4,2,1,3};

        ListNode headA = new ListNode(listA[0]);
        ListNode preA = headA;
        for (int i = 1; i < listA.length; i++) {
            ListNode temp = new ListNode(listA[i]);
            preA.next = temp;
            preA = preA.next;
        }
        ListNode ans = sortList(headA);
        while (ans != null){
            System.out.println(ans.val);
            ans = ans.next;
        }

    }
    public static ListNode sortList(ListNode head) {
        if(head == null)
            return null;
        List<Integer> list = new ArrayList<>();
        ListNode pre = head;
        while (pre != null){
            list.add(pre.val);
            pre = pre.next;
        }
        int[] values = list.stream().mapToInt(Integer::intValue).toArray();

        Arrays.sort(values);
        ListNode first = new ListNode(values[0]);
        ListNode preF = first;
        for (int i = 1; i < values.length; i++) {
            ListNode temp = new ListNode(values[i]);
            preF.next = temp;
            preF = preF.next;
        }


        return first;
    }
}
